package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private Executor threadPoolTaskExecutor;


    @Autowired
    private RedissonClient redissonClient;

    /**
     * 获取专辑详情页渲染所需参数
     *
     * @param albumId
     * @return
     */
    @Override
    public Map<String, Object> getItem(Long albumId) {
        //0.采用布隆过滤器解决缓存穿透问题
        //RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        //if(!bloomFilter.contains(albumId)){
        //    throw new GuiguException(404, "专辑不存在");
        //}

        //1.创建封装详情页map对象
        Map<String, Object> map = new ConcurrentHashMap<>();

        //2.远程调用专辑服务查询专辑信息，封装专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolTaskExecutor);


        //3.远程调用专辑服务查询分类信息，封装分类对象
        CompletableFuture<Void> categoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "分类{}不存在", albumInfo.getCategory3Id());
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolTaskExecutor);


        //4.远程调用专辑服务查询统计数值，封装统计对象
        CompletableFuture<Void> albumStatCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计{}不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolTaskExecutor);

        //5.远程调用用户服务查询主播信息，封装主播对象
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
            map.put("announcer", userInfoVo);
        }, threadPoolTaskExecutor);

        //6.采用异步编排对四项任务组合，所有异步任务都必须执行完成
        CompletableFuture.allOf(
                        albumInfoCompletableFuture,
                        categoryViewCompletableFuture,
                        albumStatCompletableFuture,
                        announcerCompletableFuture
                ).orTimeout(1, TimeUnit.SECONDS)
                .join();

        //7.响应map
        return map;
    }
    /*@Override
    public ItemVo getItem(Long albumId) {
        //1.创建封装详情页VO对象
        ItemVo itemVo = new ItemVo();

        //2.远程调用专辑服务查询专辑信息，封装专辑信息
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", albumId);
        itemVo.setAlbumInfo(albumInfo);
        //3.远程调用专辑服务查询分类信息，封装分类对象
        BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
        Assert.notNull(baseCategoryView, "分类{}不存在", albumInfo.getCategory3Id());
        itemVo.setBaseCategoryView(baseCategoryView);


        //4.远程调用专辑服务查询统计数值，封装统计对象
        AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
        Assert.notNull(albumStatVo, "专辑统计{}不存在", albumId);
        itemVo.setAlbumStatVo(albumStatVo);

        //5.远程调用用户服务查询主播信息，封装主播对象
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
        Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
        itemVo.setAnnouncer(userInfoVo);

        //5.响应vo
        return itemVo;
    }*/
}
